package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Log;
import com.itheima.domain.db.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface LogMapper extends BaseMapper<Log> {

    /**
     * 查询登录用户数和注册用户数
     *
     * @param type  操作类型
     * @param today 时间
     * @return 人数
     */
    @Select("SELECT COUNT(distinct user_id) FROM tb_log WHERE TYPE = #{type} AND log_Time = #{today}")
    Integer findByType(@Param("type") String type, @Param("today") String today);

    /**
     * 查询今日活跃人数
     *
     * @param today 日期
     * @return 活跃人数
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE log_Time = #{today}")
    Integer findActiveNum(String today);

    /**
     * 查询用户今日留存数
     *
     * @param today     今日日期
     * @param yesterday 昨天日期
     * @return 用户留存数
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE log_time=#{today}" +
            "AND user_id IN (SELECT DISTINCT user_id FROM tb_log WHERE log_time=#{yesterday})")
    Integer selectNumRetention1d(@Param("today") String today, @Param("yesterday") String yesterday);

    /**
     * 模糊查询某一天的人
     */
    @Select("select * FROM  tb_log where updated like #{date}")
    IPage<Log> findByDate(IPage Page, @Param("date") String date);

    //查询某一天的日志
    @Select("SELECT DISTINCT user_id FROM tb_log WHERE  updated LIKE #{date}")
    List<Integer> findAllByDate( String date);
}